--- a/libdjvu/DjVuText.cpp
+++ b/libdjvu/DjVuText.cpp
@@ -345,9 +345,9 @@
   int textsize = bs.read24();
   char *buffer = textUTF8.getbuf(textsize);
   int readsize = bs.read(buffer,textsize);
+  if (readsize < textsize || textsize <= 0)
+    G_THROW( ERR_MSG("DjVuText.corrupt_chunk") );
   buffer[readsize] = 0;
-  if (readsize < textsize)
-    G_THROW( ERR_MSG("DjVuText.corrupt_chunk") );
   // Try reading zones
   unsigned char version;
   if ( bs.read( (void*) &version, 1 ) == 1) 
